﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ScreenManagement
{
    class IntroScreen : MenuScreen
    {
        float fadeTimer = 0;
        float timeToShow = 3000;
        Texture2D splashTexture;
        Rectangle fullscreen;

        #region Initialization

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public IntroScreen()
            : base("")
        { }

        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            splashTexture = content.Load<Texture2D>("Textures/Screens/splash");

            fullscreen = new Rectangle(0, 0, ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (IsActive)
            {
                fadeTimer += Math.Max(gameTime.ElapsedGameTime.Milliseconds, 1);

                if (fadeTimer > timeToShow)
                {
                    Microsoft.Xna.Framework.Media.MediaPlayer.Volume = 0.15f;
                    Microsoft.Xna.Framework.Media.MediaPlayer.IsRepeating = true;
                    Microsoft.Xna.Framework.Media.MediaPlayer.Play(this.ScreenManager.backgroundMusic);
                    this.ScreenManager.AddScreen(new BackgroundScreen(), null);
                    this.ScreenManager.AddScreen(new StartScreen(), null);
                    this.ScreenManager.RemoveScreen(this);
                }
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();

            float alpha;
            if (fadeTimer < (timeToShow / 4))
                alpha = fadeTimer / (timeToShow / 4);
            else if (fadeTimer > ((timeToShow / 5) * 4))
                alpha = (timeToShow - fadeTimer) / (timeToShow / 10);
            else
                alpha = 1.0f;
            
            ScreenManager.SpriteBatch.Draw(splashTexture, fullscreen, Color.White * alpha);

            ScreenManager.SpriteBatch.End();

            base.Draw(gameTime);
        }

        #endregion

        #region Handle Input

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            const string message = "Are you sure you want to exit the game?";

            MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.Game.Exit();
        }

        #endregion
    }
}
